Page({
  data: {
    bookInfos: [],
    bookInfoIds: '',
    orderNo: '',
    shipType: '',
    shippingAddressId: ''
  },


  //邮费支付按钮
  payYoufei () {

    var _this = this
    var params = {
      orderNo: this.data.orderNo,
      bookInfoIds: this.data.bookInfoIds,
      shippingAddressId: this.data.shippingAddressId,
      shipType: this.data.shipType,
      openId: wx.getStorageSync('openid')
    }

    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/order/donate/preOrder/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          _this.wxPay(res.data.data)
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  wxPay(data) {

    // wx.navigateToMiniProgram({
    //   appId: 'wxdd8c93c8a6b5df1c',
    //   path: 'pages/sdx/index',
    //   extraData: {
    //     "orderNo": data.orderNo,
    //     "payCount": 0.01
    //   },
    //   //envVersion: 'develop',
    //   fail(res) {
    //     wx.showToast({
    //       title: res.errMsg,
    //       icon: 'none',
    //     });
    //   },
    //   success(res) {
    //     wx.showToast({
    //       title: 'ok',
    //       icon: 'none',
    //     });
    //   },
    // });

    // return
    // console.log(data.timeStamp)
    wx.requestPayment({
      timeStamp: data.timeStamp,
      nonceStr: data.nonceStr,
      package: data.package,
      signType: data.signType,
      paySign: data.sign,
      success: function (event) {
        wx.showToast({
          title: '支付成功',
          icon: 'none',
          duration: 2000,
        })
        setTimeout(function () {
          wx.navigateBack()
        },2000)
      },
      fail: function (error) {
        console.log(error)
        wx.showToast({
          title: '支付失败',
          icon: 'none'
        })
      },
      complete: function (e) {
        if (e.errMsg == 'requestPayment:ok') {
          wx.showToast({
            title: '支付成功',
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            wx.navigateBack()
          }, 2000)
        }
      }
    })
  },


  onLoad (options) {

    // console.log(options.bookInfos)
    this.setData({
      bookInfos: JSON.parse(options.bookInfos),
      bookInfoIds: options.bookInfoIds,
      orderNo: options.orderNo,
      shipType: options.shiptype,
      shippingAddressId: options.shippingAddressId
    })
  }
})
